package com.kafka.xiang.common;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.alibaba.fastjson.JSONObject;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.FileReader;
public class Keyboard {
    Robot robot;

    public Keyboard() throws AWTException {
        this.robot = new Robot();
    }

    public void enterText(String text) {
        System.out.println("Performing enter_text action with text: " + text);

        try {
            for (char c : text.toCharArray()) {
                // 获取对应字符的 ASCII 码值,在使用 Robot 类模拟键盘输入时，直接使用字符的 ASCII 码值可能会导致问题。
                //因为键盘输入通常与字符的 ASCII 码值不完全对应，特殊字符的键盘码值可能不同于 ASCII 码值。
                //int keyCode = (int) c;
                // 根据字符获取对应的 KeyEvent 常量
                int keyCode = KeyEvent.getExtendedKeyCodeForChar(c);
                // 按下和释放键
                robot.keyPress(keyCode);
                robot.keyRelease(keyCode);

                // 等待一小段时间，模拟人的输入速度
                robot.delay(100);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

        /*
        try {
            //System.out.println("Current Working Directory: " + System.getProperty("user.dir"));//输出当前目录
            // 读取 instr.json 文件内容
            JsonObject jsonObject2 = JsonParser.parseReader(new FileReader("instr.json")).getAsJsonObject();
            JsonObject jsonObject=jsonObject2.getAsJsonObject("CODES");
            //System.out.println(jsonObject.toString());
            for (char c : text.toCharArray()){
                String singlechar =String.valueOf(c);
                // 根据输入的 key 查找对应的 value
                if (jsonObject.has(singlechar)) {
                    int keyCode1 = jsonObject.get(singlechar).getAsInt();

                    // 按下和释放键
                    robot.keyPress(keyCode1);
                    robot.keyRelease(keyCode1);

                    System.out.println("Key pressed: " + singlechar + " (ASCII Code: " + keyCode1 + ")");

                } else {
                    System.out.println("Key not found in instr.json: " + singlechar);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

         */

    }

    public void enter(String key1) {
        try {
            //System.out.println("Current Working Directory: " + System.getProperty("user.dir"));//输出当前目录
            // 读取 instr.json 文件内容
            JsonObject jsonObject2 = JsonParser.parseReader(new FileReader("instr.json")).getAsJsonObject();
            JsonObject jsonObject=jsonObject2.getAsJsonObject("CODES");
            //System.out.println(jsonObject.toString());
            // 根据输入的 key 查找对应的 value
            if (jsonObject.has(key1)) {
                int keyCode1 = jsonObject.get(key1).getAsInt();

                // 按下和释放键
                robot.keyPress(keyCode1);
                robot.keyRelease(keyCode1);

                System.out.println("Key pressed: " + key1 + " (ASCII Code: " + keyCode1 + ")");

            } else {
                System.out.println("Key not found in instr.json: " + key1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void enterFn(String key1, String key2) {
        try {
            //System.out.println("Current Working Directory: " + System.getProperty("user.dir"));//输出当前目录
            // 读取 instr.json 文件内容
            JsonObject jsonObject2 = JsonParser.parseReader(new FileReader("instr.json")).getAsJsonObject();
            JsonObject jsonObject=jsonObject2.getAsJsonObject("CODES");
            //System.out.println(jsonObject.toString());
            // 根据输入的 key 查找对应的 value
            if (jsonObject.has(key1) && jsonObject.has(key2)) {
                int keyCode1 = jsonObject.get(key1).getAsInt();
                int keyCode2 = jsonObject.get(key2).getAsInt();

                // 按下和释放键
                robot.keyPress(keyCode1);
                robot.keyPress(keyCode2);
                robot.keyRelease(keyCode1);
                robot.keyRelease(keyCode2);

                System.out.println("Key pressed: " + key1 + " (ASCII Code: " + keyCode1 + ")");
                System.out.println("Key pressed: " + key2 + " (ASCII Code: " + keyCode2+ ")");

            } else {
                System.out.println("Key not found in instr.json: " + key1+"  "+key2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void enterFn(String key1, String key2, String key3) {
        try {
            // 读取 instr.json 文件内容
            JsonObject jsonObject2 = JsonParser.parseReader(new FileReader("instr.json")).getAsJsonObject();
            JsonObject jsonObject=jsonObject2.getAsJsonObject("CODES");
            //System.out.println(jsonObject.toString());
            // 根据输入的 key 查找对应的 value
            if (jsonObject.has(key1) && jsonObject.has(key2) && jsonObject.has(key3)) {
                int keyCode1 = jsonObject.get(key1).getAsInt();
                int keyCode2 = jsonObject.get(key2).getAsInt();
                int keyCode3 = jsonObject.get(key3).getAsInt();

                // 创建 Robot 对象
                Robot robot = new Robot();

                // 按下和释放键
                robot.keyPress(keyCode1);
                robot.keyPress(keyCode2);
                robot.keyPress(keyCode3);

                robot.keyRelease(keyCode1);
                robot.keyRelease(keyCode2);
                robot.keyRelease(keyCode3);

                System.out.println("Key pressed: " + key1 + " (ASCII Code: " + keyCode1 + ")");
                System.out.println("Key pressed: " + key2 + " (ASCII Code: " + keyCode2+ ")");
                System.out.println("Key pressed: " + key3 + " (ASCII Code: " + keyCode3+ ")");

            } else {
                System.out.println("Key not found in instr.json: " + key1+"  "+key2+"  "+key3);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
